package com.gwsdk.paysdk.acitvityV2.fragment;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.genwan.smartrefresh.layout.SmartRefreshLayout;
import com.genwan.smartrefresh.layout.api.RefreshLayout;
import com.genwan.smartrefresh.layout.footer.ClassicsFooter;
import com.genwan.smartrefresh.layout.header.ClassicsHeader;
import com.genwan.smartrefresh.layout.listener.OnLoadMoreListener;
import com.genwan.smartrefresh.layout.listener.OnRefreshListener;
import com.gwsdk.paysdk.adapter.GWGamePayListAdapter;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.entity.GamePayRecordEntity;
import com.gwsdk.paysdk.http.process.PayRecordProcess;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.ToastUtil;

import java.util.ArrayList;

public class GWBillFragment extends GWBaseFragment{

    private ImageView backItem;

    private ListView listView;

    private View layoutHaveData;
    private View tvGwNodata;

    private View progressView;
    private SmartRefreshLayout smartRefresh;
    ArrayList<GamePayRecordEntity.ListsBean> objects = new ArrayList<>();

    private GWGamePayListAdapter adapter;

    private int pager = 1;
    private PayRecordProcess payRecordProcess;

    @Override
    public int setContentView() {
        return GWInflaterUtils.getLayout(getActivity(),"gw_fragment_bill");
    }

    @Override
    public void initView(View view) {
        backItem = view.findViewById(GWInflaterUtils.getControl(getActivity(),"btn_back"));
        backItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        layoutHaveData = view.findViewById(GWInflaterUtils.getControl(getActivity(),"layout_haveData"));
        tvGwNodata = view.findViewById(GWInflaterUtils.getControl(getActivity(),"not_data_view"));
        smartRefresh = view.findViewById(GWInflaterUtils.getControl(getActivity(),"SmartRefresh"));
        progressView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"progress_view"));
        smartRefresh.setRefreshHeader(new ClassicsHeader(getActivity()));
        smartRefresh.setRefreshFooter(new ClassicsFooter(getActivity()));
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pager = 1;
                objects.clear();
                payRecordProcess.setLimit(pager+"");
                payRecordProcess.post(payRecordhandler);
            }
        });
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                pager = pager +1;
                payRecordProcess.setLimit(pager+"");
                payRecordProcess.post(payRecordhandler);
            }
        });


        adapter = new GWGamePayListAdapter(getActivity(),objects);

        listView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"list_view"));
        listView.setAdapter(adapter);

        payRecordProcess = new PayRecordProcess(getActivity());
        payRecordProcess.setLimit(pager+"");
        payRecordProcess.post(payRecordhandler);
    }

    @SuppressLint("HandlerLeak")
    Handler payRecordhandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            progressView.setVisibility(View.GONE);
            switch (msg.what) {
                case Constant.PAY_RECORD_SUCCESS:
                    smartRefresh.finishLoadMore();
                    smartRefresh.finishRefresh();
                    GamePayRecordEntity gamePayRecordEntity = (GamePayRecordEntity)msg.obj;
//                    tv_money.setText("￥"+gamePayRecordEntity.getCount());
                    if(gamePayRecordEntity.getLists().size()==0){
                        if(objects.size()==0){
                            layoutHaveData.setVisibility(View.GONE);
                            tvGwNodata.setVisibility(View.VISIBLE);
                        }else{
                            ToastUtil.show(getActivity(),"已经到底了");
                        }
                    }else{

                        layoutHaveData.setVisibility(View.VISIBLE);
                        objects.addAll(gamePayRecordEntity.getLists());
                        adapter.notifyDataSetChanged();
                        if(objects.size()==0){
                            layoutHaveData.setVisibility(View.GONE);
                            tvGwNodata.setVisibility(View.VISIBLE);
                        }else {
                            layoutHaveData.setVisibility(View.VISIBLE);
                            tvGwNodata.setVisibility(View.GONE);
                        }
                    }
                    break;
                case Constant.PAY_RECORD_FAIL:
                    smartRefresh.finishLoadMore();
                    smartRefresh.finishRefresh();
                    if(objects.size()==0){
                        layoutHaveData.setVisibility(View.GONE);
                        tvGwNodata.setVisibility(View.VISIBLE);
                    }
                    break;
            }
        }
    };

    @Override
    public void getData() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
